{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### SimpleImputer\n",
    "\n",
    "SimpleImputer 是 Scikit-learn 提供的一个用于处理缺失值的工具。它通过简单的策略（如均值、中位数、众数或常数）来填充数据集中的缺失值（NaN）。SimpleImputer 是数据预处理中处理缺失值的常用方法之一。\n",
    "\n",
    "相关参数：\n",
    "\n",
    "missing_values: 指定缺失值的表示形式，默认为 np.nan。\n",
    "\n",
    "strategy: 填充策略，可选值包括：\n",
    "\n",
    "    mean: 使用特征的均值填充（仅适用于数值数据）。\n",
    "\n",
    "    median: 使用特征的中位数填充（仅适用于数值数据）。\n",
    "\n",
    "    most_frequent: 使用特征的众数填充（适用于数值和类别数据）。\n",
    "\n",
    "    constant: 使用常数填充，需配合 fill_value 参数。\n",
    "\n",
    "fill_value: 当 strategy=\"constant\" 时，指定填充的常数值。\n",
    "\n",
    "copy: 是否复制数据，默认为 True。\n",
    "\n",
    "主要方法：\n",
    "\n",
    "fit(X): 计算每个特征的填充值（如均值、中位数等）。\n",
    "\n",
    "transform(X): 使用计算出的填充值替换缺失值。\n",
    "\n",
    "fit_transform(X): 先拟合数据，再进行填充"
   ],
   "id": "4cc92d91dc518ae9"
  },
  {
   "cell_type": "code",
   "id": "6b26da29b160bcf8",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-09T16:51:06.321259Z",
     "start_time": "2025-01-09T16:51:06.317693Z"
    }
   },
   "source": [
    "from sklearn.impute import SimpleImputer\n",
    "import numpy as np\n",
    "\n",
    "# 示例数据（包含缺失值）\n",
    "data = np.array([[1, 2], [np.nan, 3], [7, 6]])\n",
    "print(\"原始数据:\\n\", data)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据:\n",
      " [[ 1.  2.]\n",
      " [nan  3.]\n",
      " [ 7.  6.]]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "使用均值填充缺失值",
   "id": "a78e56f217852d9a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T16:51:03.753919Z",
     "start_time": "2025-01-09T16:51:03.749423Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 SimpleImputer 对象，使用均值填充\n",
    "imputer = SimpleImputer(strategy='mean')\n",
    "\n",
    "# 拟合并转换数据\n",
    "imputed_data = imputer.fit_transform(data)\n",
    "\n",
    "print(\"填充后的数据:\\n\", imputed_data)"
   ],
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "填充后的数据:\n",
      " [[1. 2.]\n",
      " [4. 3.]\n",
      " [7. 6.]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "使用常数填充缺失值",
   "id": "f85ba153717c5bf7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-09T16:51:01.311007Z",
     "start_time": "2025-01-09T16:51:01.306181Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 SimpleImputer 对象，使用常数填充\n",
    "imputer = SimpleImputer(strategy='constant', fill_value=0)\n",
    "\n",
    "# 拟合并转换数据\n",
    "imputed_data = imputer.fit_transform(data)\n",
    "\n",
    "print(\"填充后的数据:\\n\", imputed_data)"
   ],
   "id": "db02f135d1d6bec4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "填充后的数据:\n",
      " [[1. 2.]\n",
      " [0. 3.]\n",
      " [7. 6.]]\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
